package com.woniuxy.amusementparkos.controller.wyh;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.amusementparkos.entity.wyh.Balance_Change;
import com.woniuxy.amusementparkos.entity.wyh.Customer;
import com.woniuxy.amusementparkos.entity.wyh.WristStrap_Info;
import com.woniuxy.amusementparkos.entity.wyh.WristStrap_Repository;
import com.woniuxy.amusementparkos.service.wyh.Balance_ChangeService;
import com.woniuxy.amusementparkos.service.wyh.CustomerService;
import com.woniuxy.amusementparkos.service.wyh.WristStrap_InfoService;
import com.woniuxy.amusementparkos.service.wyh.WristStrap_RepositoryService;


/**
 * 腕带相关操作通过该 controller 包括腕带仓库读取类型、制作腕带
 * 
 * @author wyh
 *
 */
@Controller
public class WriststrapController {
	@Autowired
	WristStrap_RepositoryService RepositoryService;

	@Autowired
	WristStrap_InfoService wristStrap_InfoService;

	@Autowired
	CustomerService customerService;
	
	@Autowired
	Balance_ChangeService balance_ChangeService;
	/*
	 * 腕带中心
	 */
	@RequestMapping("/myindex")
	public String htmlTest() {
		return "wyh/腕带中心";

	}
	
	/*
	 * 客服中心
	 */
	@RequestMapping("/service")
	public String service() {
		return "wyh/客服中心";

	}
	
	/*
	 * 收银中心
	 */
	@RequestMapping("/cashier")
	public String cashier() {
		return "wyh/收银中心";

	}

	/*
	 * 跳转腕带发放
	 */
	@RequestMapping("/create")
	public String crateWristStrap() {

		return "wyh/腕带发放";

	}
	
	/*
	 * 腕带充值
	 */
	@RequestMapping("/pay")
	public String charge() {
		return "wyh/腕带充值";
	}

	// 根据id返回仓库的腕带对象
	@ResponseBody
	@RequestMapping("/gettype")
	public Object getTypeById(String wriststrap_id) {
		System.out.println("运行了该代码！");
		System.out.println(wriststrap_id);
		WristStrap_Repository wristStrap = RepositoryService.getWristStrap(wriststrap_id);
		System.out.println(wristStrap);
		return wristStrap;
	}

	// 充值支付

	@ResponseBody
	@RequestMapping("/payamount")
	public String payamount(String wriststrp_id,Double cash, Double bankcard, Double alipay) {
		
		System.out.println("info_id-------"+wriststrp_id);
		System.out.println("-------现金充值金额----" + cash);
		// 如果不为空，就存入balance—change
		
		//后端数据验证  编号不能为空  
		if(wriststrp_id==null) {
			return "编号不能为空";
		}
		WristStrap_Info currentWS;
		//根据腕带编号，查询该正在使用腕带信息
		try {
			currentWS = wristStrap_InfoService.getWorkStateWristStrap(wriststrp_id);
		} catch (Exception e) {
				//表示失败
				return "该用户不存在";
		}
		
		//腕带的状态不为0  非使用状态
		if(currentWS.getInfo_state()!=0) {
			return "腕带状态不合法";
		}
		
		//获取infoid
		int info_id=currentWS.getInfo_id();
		try {
			//现金充值
			if (cash != null) {
				Balance_Change balance_Change = new Balance_Change(null,info_id,cash,1,null,1,1,2);
				//加入资金变动项
				balance_ChangeService.addObject(balance_Change);
				
				
				
				//余额变动后的值
				BigDecimal newbalance = currentWS.getBalance().add(new BigDecimal(cash));
				
				
				
				//新的余额更新到腕带信息表
				wristStrap_InfoService.changeBalacne(info_id, newbalance);
			}
			//银行卡充值
			if (bankcard != null) {
				//获取最新腕带信息
				currentWS = wristStrap_InfoService.getWorkStateWristStrap(wriststrp_id);
				
				//新增腕带余额变动信息
				Balance_Change balance_Change = new Balance_Change(null,info_id,bankcard,2,null,1,1,2);
				balance_ChangeService.addObject(balance_Change);
			
				
				
				//余额变动后的值
				BigDecimal newbalance = currentWS.getBalance().add(new BigDecimal(bankcard));
				
				//更新到腕带信息表
				wristStrap_InfoService.changeBalacne(info_id, newbalance);
			}
			//支付宝充值
			if (alipay != null) {
				//获取最新腕带信息
				currentWS = wristStrap_InfoService.getWorkStateWristStrap(wriststrp_id);
				
				Balance_Change balance_Change = new Balance_Change(null,info_id,alipay,2,null,1,1,2);
				balance_ChangeService.addObject(balance_Change);
				
				
				
				//余额变动后的值
				BigDecimal newbalance = currentWS.getBalance().add(new BigDecimal(alipay));
				
				//更新到腕带信息表
				wristStrap_InfoService.changeBalacne(info_id, newbalance);
			}
		} catch (Exception e) {
			return "充值失败";
		}
		
		return "成功";

	}

	/*
	 * 生成腕带信息
	 */
	@ResponseBody
	@RequestMapping("/crateWristStrap")
	public Object crateWristStrap_info(WristStrap_Info wriststrap) {

		 System.out.println(wriststrap);
		// 保存客户信息到customer;
		// 创建当前customer对象
		Customer newcustomer = new Customer(11, wriststrap.getCustomer().getTel(),
				wriststrap.getCustomer().getCustomer_name(), new BigDecimal(10000), 1,new Date());

		System.out.println("新创建的顾客详细信息"+newcustomer);
		// 新建的客户加入到customer表中
		int id;
		try {
			//插入顾客信息返回id
			id = customerService.addBackId(newcustomer);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			System.out.println("添加失败！");
			return null;
		}

		//设置实际id
		newcustomer.setCustomer_id(id);
		
		System.out.println("客户完整信息——--"+newcustomer);
		// 设置完整的客户信息到腕带信息
		wriststrap.setCustomer(newcustomer);
		
		
		//默认创建后正常使用
		wriststrap.setInfo_state(0);
		// 设置其他属性 这些属性从session中取登陆状态
		wriststrap.setOperater_id(1);
		wriststrap.setPos_id(1);
		
		System.out.println("腕带详细信息"+wriststrap);
		// 异常处理
		try {
			
			//修改腕带仓库该腕带的使用状态
			int state=1;//正在使用状态
		RepositoryService.updateState(wriststrap.getWriststrp_id(), state);
			
			//新增腕带信息到数据库 并返回生成的info id;
			int infoid = wristStrap_InfoService.insertBackInfoid(wriststrap);
		
			System.out.println("++++++infoid"+infoid);
			
			//腕带生成后生成固定消费项目  押金10 ; 支付方式：3押金状态。支付状态成功   0 ，1；			
			Balance_Change balance_Change=new Balance_Change(null, infoid, -10.00, 3, null, 3, 0, 2);
			
			System.out.println("腕带余额变动信息："+balance_Change);
			//加入到数据库
			balance_ChangeService.addObject(balance_Change);
			
			//更新腕带balance
			try {
				
				//更新腕带信息
				wristStrap_InfoService.changeBalacne(infoid, wriststrap.getBalance().subtract(new BigDecimal(10)));

			} catch (Exception e) {
				
				System.out.println("腕带余额信息修改有误");
			}
			
			
			System.out.println(infoid);
//			返回对象新插入的腕带信息对象
		
			//获取该最新腕带信息
			WristStrap_Info newWristStrap_Info = wristStrap_InfoService.getByInfoId(infoid);
			System.out.println("新创建腕带信息为------"+newWristStrap_Info);
			return newWristStrap_Info;
			
		} catch (Exception e) {
			System.out.println();
			return null;
		}

	}


}
